﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace Microsoft.Coding4Fun.TweetCraft.Targa
{
    [StructLayout(LayoutKind.Explicit, Size = 18)]
    internal struct TargaHeader
    {
        public TargaHeader(int width, int height, int pixelDepth)
        {
            imageType = TargaImageType.UncompressedRgb;
            imageIdLength = 0;
            colorMapType = 0;
            colorMapOffset = 0;
            colorMapLength = 0;
            colorMapPixelDepth = 0;
            imageDescriptor = 0;

            originX = 0;
            originY = 0;

            this.width = (Int16)width;
            this.height = (Int16)height;

            this.pixelDepth = (Byte)pixelDepth;
        }

        [FieldOffset(0)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Byte imageIdLength;

        [FieldOffset(1)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Byte colorMapType;

        [FieldOffset(2)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private TargaImageType imageType;

        // Color Map Specification
        [FieldOffset(3)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        [SuppressMessage("Microsoft.Portability", "CA1900", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Int16 colorMapOffset;

        [FieldOffset(5)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        [SuppressMessage("Microsoft.Portability", "CA1900", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Int16 colorMapLength;

        [FieldOffset(7)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Byte colorMapPixelDepth;

        [FieldOffset(8)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Int16 originX;

        [FieldOffset(10)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Int16 originY;

        [FieldOffset(12)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Int16 width;

        [FieldOffset(14)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Int16 height;

        [FieldOffset(16)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Byte pixelDepth;

        // Image Descriptor
        [FieldOffset(17)]
        [SuppressMessage("Microsoft.Performance", "CA1823", Justification = "Field is used for explicit structure layout to map to the binary header found in Truevision Targa image files.")]
        private Byte imageDescriptor;
    }
}